package com.fahai.web.service.impl;

import com.fahai.common.PageVo;
import com.fahai.web.dao.SysUserDao;
import com.fahai.web.dao.UserGroupDao;
import com.fahai.web.dao.UserRoleDao;
import com.fahai.web.entity.SysUser;
import com.fahai.web.entity.UserRole;
import com.fahai.web.service.SysUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * Created by sky on 17/5/9.
 * 用户角色和权限设置，详见项目README.md说明文档
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    SysUserDao sysUserDao;

    @Autowired
    UserGroupDao userGroupDao;

    @Autowired
    UserRoleDao userRoleDao;
    /**
     * 根据用户名查找用户
     * @param username
     * @return
     */
    @Override
    public SysUser findUserInfo(String username){
        SysUser sysUser = sysUserDao.selectByUserName(username);
        return sysUser;
    }

    /**
     * 查询用户列表
     *
     * @param vo
     * @param pageVo
     * @return
     */
    @Override
    public PageVo findSysUserList(SysUser vo, PageVo pageVo) {
        List<SysUser> vos = sysUserDao.select(vo);
        pageVo.setResult(vos);
        return pageVo;
    }

    /**
     * 更新用户权限和信息
     */
    @Override
    public void updateSysUser(SysUser su){
        sysUserDao.updateBySinglePK(su);

    }

    @Override
    public void saveSysUser(SysUser su){
        sysUserDao.insertBySinglePk(su);

    }

    @Override
    public List<UserRole> findAllUserRole(){
        return userRoleDao.selectAll();

    }

    @Override
    public PageVo selectUserList(PageVo pageVo) {
        Page<Map<String,Object>> page = PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize());
        List<Map<String,Object>> getData =sysUserDao.selectUserList();
        pageVo.setResult(getData);
        pageVo.setTotal(sysUserDao.selectUserCount());
        return pageVo;
    }

    @Override
    public Long selectUserByNameCount(String userName) {

        return sysUserDao.selectUserByNameCount(userName);
    }


    @Override
    public int accountCount(SysUser sysUser) {
        return  sysUserDao.selectCount(sysUser);
    }


    @Override
    public SysUser findSysUserByUserName(String userName) {
        return sysUserDao.selectByUserName(userName);
    }
}
